package 冯奕龙;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;


public class Picture {

    /**
     * @param args
     */
    public static void main(String[] args) {

        new Picture().testPictureMark();
    }

    public void testPictureMark() {

        File srcImageFile = new File("D:\\代码\\JavaDemo1\\src\\test\\java\\冯奕龙\\1.jpg");
        File logoImageFile = new File("D:\\代码\\JavaDemo1\\src\\test\\java\\冯奕龙\\logo.jpg");

        File outputRoateImageFile = new File("D:\\代码\\JavaDemo1\\src\\test\\java\\冯奕龙\\desktop_pic_mark.jpg");

        createWaterMarkByIcon(srcImageFile, logoImageFile, outputRoateImageFile);
    }

    public void createWaterMarkByIcon(File srcImageFile, File logoImageFile,
                                      File outputImageFile) {

        OutputStream os = null;
        try {
            Image srcImg = ImageIO.read(srcImageFile);

            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

            Graphics2D graphics = buffImg.createGraphics();
            graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics.drawImage(srcImg, 0, 0, null);

            ImageIcon logoImgIcon = new ImageIcon(ImageIO.read(logoImageFile));
            Image logoImg = logoImgIcon.getImage();


            /**
             * SRC
             * SRC_IN
             * SRC_OUT
             * SRC_OVER
             * 按照为都认识，所谓源模式，也就是将源(Source，以下简称src)放在上面，目标放在下面(Destination，以下简称dst)，in便是忽略掉src再dst的外面部分，out则是忽略掉里面部分，over则是覆盖在dst的上面
             * DST_IN
             * DST_OUT
             * DST_OVER
             * 所谓目标模式，就是与源模式相反，将源模式放在下面，将目标模式放在上面，in便是忽略掉dst都与源的飞重叠部分，而out便是忽略掉dst的重叠部分，over自然是将dst覆盖在上面
             * CLEAR
             * 顾名思义，就是清楚掉一部分。。。但是清楚的确实源部分，这个需要记忆一下
             * 下面是sun提供的演示
             */
            float alpha = 0.9f; // 透明度
            graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, alpha));

            //将水印照片缩小为目标图片的5倍，将它显示在目标图片的右下角，计算公式是
            //计算目标图片的宽度和高度，用目标图标的宽度和高度减去水印图片的宽高就是显示的位置
            int logoImgWidth = buffImg.getWidth() / 5;
            int logoImgHeight = buffImg.getHeight() / 5;
            int x = buffImg.getWidth() - logoImgWidth;
            int y = buffImg.getHeight() - logoImgHeight;
            graphics.drawImage(logoImg, x, y, logoImgWidth, logoImgHeight, null);
//            graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_IN));
            graphics.dispose();

            os = new FileOutputStream(outputImageFile);
            // 生成图片
            ImageIO.write(buffImg, "JPG", os);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

